﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace ECCIT
{
    public class StatusQueueMonitorController
    {
        private static StatusQueue oStatusQueue = new StatusQueue();
        public static void Add(string statusMessage)
        {
            if (statusMessage == null)
                return;

            lock (oStatusQueue)
            {
                oStatusQueue.Enqueue(statusMessage);
            }
        }

        private StatusOutputHandler _handler;
        public StatusQueueMonitorController(StatusOutputHandler handler)
        {
            _handler = handler;
        }

        public void Run()
        {
            try
            {
                bool bContinue = true;

                string statusMessage = null;
                while (bContinue)
                {
                    lock (oStatusQueue)
                    {
                        while (oStatusQueue.Count > 0)
                        {
                            statusMessage = (string)oStatusQueue.Dequeue();
                            if (statusMessage != null && _handler != null)
                                _handler(statusMessage);
                        }
                    }
                    
                    Thread.Sleep(100);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public void Deregister()
        {
            Application.ExitThread();
        }
    }
}
